global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/import_minimumwage.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {


*****************************
* This file imports minimum wages from the OECD and other sources.
*
* We use hourly wages as provided by the OECD and merge in US, UK, Germany and some missing years for Ireland


***************************

*load all codes wages to merge later
use ${final_dir}/wage_combined_final_allcodes.dta, clear
keep if code=="TOT" | code=="D"
tempfile wages_all_codes
save "`wages_all_codes'", replace


******************************************
*Import OECD data and correct some things
******************************************
*Minimum wages Annual, hourly, monthly (different format to other files) | from OECD.Stat | Downlaoded June 24th 2019
import excel ${mow_data_raw}\Minimum_wages\min_wage_all.xlsx, sheet("OECD.Stat export") cellrange(A3:I685) firstrow clear

rename Payperiod Country
drop if _n==1
rename B year
rename C currency

replace Country=Country[_n-1] if Country==""
replace currency=currency[_n-1] if currency==""
destring Hourly, replace force
destring Daily, replace force
destring Weekly, replace force
destring Monthly, replace force 
destring Annual, replace force
destring year, replace force

* We prefer hourly. 
* Go through countries one by one
* we have some countries for which we don't have hourly 

count if Hourly==. & Monthly==. & Daily!=.

*The minimum wage in Turkey is per month (https://countryeconomy.com/national-minimum-wage/turkey)
*It seems like the OECD just multiplied monthly by 12. 
*According to this website (http://turkishlaborlaw.com/news/legal-news/650-how-to-calculate-hourly-wage)
*we should divide monthly by 225 for the hourly wage.

replace Hourly=Annual/12/225 if Country=="Turkey"

*Mexico, according to Wipedia: https://www.wikiwand.com/en/List_of_minimum_wages_by_country
* Due to vacation workers are actually paid for 56 hours although a work week is 48 (working 6 days a week)
* Consequently

replace Hourly=Daily/8*(56/48) if Country=="Mexico"

tabulate Country if Hourly==. & Annual!=. 

* Belgium (https://www.expatica.com/be/employment/employment-law/a-guide-to-minimum-wage-in-belgium-and-average-salary-972342/)
* has a 38-hour work week, 4 weeks paid annual leave (http://employment.belgium.be/en/themes/international/posting/working-conditions-be-respected-case-posting-belgium/minimum-paid)
* and ~10 public holidays (https://www.nbb.be/en/about-national-bank/national-bank-belgium/public-holidays)

replace Hourly=Annual/(52*38*(52-6)/52) if Country=="Belgium"

* Spain: Here OECD is wrong. Payment is 14 times a year but they have yearly/monthly=12. 
* Work week of 38 hours. (https://www.expatica.com/es/employment/employment-law/wages-104545/)
* Workers have 6 weeks off. 

replace Hourly=Monthly/(52/12*38*(52-6)/52)*14/12 if Country=="Spain"

* Netherlands (https://www.government.nl/topics/minimum-wage/amount-of-the-hourly-minimum-wage) 
* Again, only monthly salary exists 

replace Hourly=Monthly/(52/12*38*(52-6)/52) if Country=="Netherlands"

* Brazil: 4 weeks per year (https://vacationtracker.io/leave-laws/south-america/brazil/)
* with a 44 hour work week: https://wageindicator.org/salary/minimum-wage/brazil

replace Hourly=Monthly/(52/12*44*(52-6)/52) if Country=="Brazil"

tabulate Country if Hourly==. & Annual!=.

* Israel got a (monthly) minimu wage in 1995. The OECD appears just to divide the monthly minimum wage
* by 186 but they only do so back to 2000. There appears not to be an hourly minimum wage in Israel. 
* Wikipedia (https://www.wikiwand.com/en/Israeli_labor_law) states that workers work 43 hours a week. 
*Nothing on vacations: 

replace Hourly=Monthly*12/52/43 if Country=="Israel"

tabulate Country if year==2000 & Hourly==.

* There remain four countries: 
* Germany (seperately constructed, see details in Construct_minimumwages_GER_US_UK_IE.do)
* Poland, Portugal and Russia

* Poland has a monthly minimum wage. Poland has a 40 hour work week. https://boundlesshq.com/guides/poland/hours-of-work/

replace Hourly=Monthly*(12/52)/40*(30/29) if Country=="Poland"

* Portugal has a monthly minimum wage but has 14 monthly payments . 
* (https://www.expatica.com/pt/employment/employment-law/minimum-wage-in-portugal-a-guide-to-average-salary-in-portugal-927429/)
* A 40 hour work week and 22 days off a year (about twice a month of 8 hours)
* (https://businessculture.org/southern-europe/business-culture-in-portugal/work-life-balance-in-portugal/)
* Consequently: 

replace Hourly=Monthly*(12/52)/40*(160/144) if Country=="Portugal" 

* Russian federation has only had minimum wages since 2000. 

* Do we have it for the full period for all large countries 

tabulate Country if year==1995 & Hourly==.
tabulate Country if year==2009 & Hourly==.

* United Kingdom and Ireland do not have minimum wages back in Time. 
* The UK only has an official minimum wage from 1999 (though the OECD appears only to have it from 2000). 
* From 1909 to 1993 the UK had wage councils (see details in Construct_minimumwages_GER_US_UK_IE.do and reduce_UK_minw.do). 
* We impute the years 1993 to 1999.

tempfile OECD_min_wages
save `OECD_min_wages', replace

*Import the UK minwage and go from there
use ${mow_data_proc}/UK_minw.dta, clear
rename country Country
merge 1:1 year Country using `OECD_min_wages', nogen
drop if year<1995

* The min_wage from this file is the weekly minimum wage multiplied by 50. UK has a 40 hour work week 
* There is a 28 day holiday minimum (https://www.wikiwand.com/en/Working_time_in_the_United_Kingdom). That's about 20 hours a month
replace Hourly=min_wage/(40*50)*(160/140) if Country=="United Kingdom"

* And then use wages for the Germany and Ireland (just adding ones before 2000)
rename Country country

merge 1:1 country year using ${mow_data_proc}/Germany_minw.dta
drop _merge
drop if country=="Ireland" & year<2000
merge 1:1 country year using ${mow_data_proc}/minwage_hourly_IE.dta
rename country Country

* German minimum wages are indexed. There is an official 
* minimum wage in 2014 was set at 8.50 so we rescale
replace min_wage_germany=min_wage_germany/159.13*8.5
replace Hourly=min_wage_germany if Country=="Germany"

*Ireland had no official minimum wage until 2000. But similar to the UK, 
*we have wage agreements between unions, employers and the government starting in the 1980s
*(see details in Construct_minimumwages_GER_US_UK_IE.do)
replace Hourly= minwage_IE if Country=="Ireland" & year<2000


* Additional countries
* Finland does not have legally binding minimum wages: (https://tyosuojelu.fi/en/employment-relationship/pay)
* Indonesia seems not  have a minimum wage back in time
* There is no minimum wage in Italy
* There was no minimum wage in India at the time. 

keep Country year Hourly
sort Country year
tempfile min_wages_complete
save "`min_wages_complete'", replace

*add country codes
import excel ${mow_data_raw}/countries/country_code.xlsx, clear firstrow
merge 1:m Country using `min_wages_complete', nogen
* We are losing Colombia 
tabulate Country if country=="" 
drop if country==""
drop if Country==""

*add the lowskill wage and the Total and manufacturing code 
merge 1:m country year using `wages_all_codes', keepusing(ls_wage code)
keep if code=="D"

sort country year
label var Hourly "hourly min. wage"
drop _merge

*add the minimum wages for the US.
merge 1:1 country year using ${mow_data_proc}/US_minw.dta
drop _merge

replace Hourly = hourly_usa if country =="usa"
drop hourly_usa

save ${mow_data_proc}\wage_combined_final_with_min_wage.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat
			
			


